<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	     <title>产品分配管理</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
		<link rel="stylesheet" type="text/css" href="<%=basePath%>CSS/layout.css" />
	    <link rel="stylesheet" type="text/css" href="<%=basePath%>CSS/jquery.autocomplete.css" />
  </head>
  <body>
    <div id="wrapper">
    <!--头部菜单开始-->
    <jsp:include page="/common/head.jsp"></jsp:include>
    <!--头部菜单结束-->
	<div id="BreadCrumb">我的位置：产品管理 &gt; 产品分配管理</div>
	<div id="REC_content">
	    <fieldset id="BasicInfo" class="mainField">
		    <legend><span class="title">产品分配管理</span></legend>
			<div class="innerCon">
			    <div class="mainSearchBox">
                <form action="wip.do" method="post">
                   <c:if test="${not empty product.routeId}"><input type="hidden" name="routeType" value="${product.routeId}"/></c:if>	
			       <table width="100%" cellpadding="0" cellspacing="0" border="0" class="editTab">
					<tr height="35">
						<td width="120" class="title">条码</td>
						<td align="left" colspan="5" >
						  <input id="pdtId" name="pdtId" type="text" onblur="settedPdtId()" size="32"/>
						</td>
					</tr>
					<tr height="35">
						<td width="120" class="title">工厂SKU号</td>
						<td align="left" colspan="5">
						  <input id="proId" name="proId" type="text" onblur="settedProId()" size="48" value="${proSku}"/>
						  <p><span  id="proIds"></span></p>
						</td>
					</tr>
					<tr height="35">
						<td width="120" class="title">客户Sku号</td>
						<td align="left" colspan="5">
						  <input id="skuNo" name="skuNo" type="text" onblur="settedSkuNo()" value="${product.cmtpdtskuid}"/>
						</td>
					</tr>
					<tr id="moresearchCon">
						<td class="title" height="30">款号</td>
						<td align="left"><input id="styleNo" name="styleNo" type="text" readonly="readonly" value="${product.styleno}"/></td>
						<td width="80" class="title">路由选择</td>
						<td align="left">
						  <select id="routeType" name="routeType" onchange="getRouteSetting()" <c:if test="${not empty product.routeId}">disabled="disabled"</c:if>>
                            <option></option>
                          <c:forEach var="typeName" items="${routeTypes}">
                            <c:if test="${typeName != product.routeId}"><option>${typeName}</option></c:if>
                            <c:if test="${typeName eq product.routeId}"><option selected="selected">${typeName}</option></c:if>
                         </c:forEach>
                         </select>
                         <c:if test="${empty product.routeId}">
                           <span><input type="button" value="设置路由" id="regRouteButton" onclick="regRoute();"/></span>
                         </c:if>
                         <span id="suggest">${suggest}</span>
					</td>
					</tr>
					<tr id="moresearchCon">
						<td colspan="6">
						  &nbsp;
						</td>
					</tr>
					<tr id="moresearchCon" align="center">
						<td colspan="6">
						  <div id="result"></div>
						</td>
					</tr>
					<tr id="moresearchCon">
						<td colspan="6">
						  &nbsp;
						</td>
					</tr>
					<tr id="moresearchCon">
						<td colspan="6">
						  <input id="seach" class="newSearchButton" type="button" value="查询"  onclick="getWip()"/>
                          <input id="subBut" class="newSearchButton" type="submit" value="提交" disabled=disabled/>
						</td>
					</tr>
				</table>
				</form>
			    
			    </div>
			    <!-- begin tab -->
			    <div class="topControl clearfix">
				    <div id="JobsMgrTabCon" class="itemTabCon">
					    <ul id="JobsMgrTabs" class="itemTabs">
					        <li id="dueTab" class="blur"><a href="<%=basePath%>routeSetting.do">路由预设管理</a></li>
							<li id="dueTab" class="focus"><a href="<%=basePath%>wip.do" onfocus="this.blur()">产品分配管理</a></li>
							<li id="dueTab" class="blur"><a href="<%=basePath%>orderWip.do">在制产品管理</a></li>
					    </ul>
				    </div>
			    </div>
			    
			    
			    <!-- end tab -->
				<div id="mainListCon">
				    <div id="mLC">
					    <ul>
						    <li id="wipResult">
							</li>
						</ul>
					</div>
				</div>
			</div>
		</fieldset>
	</div>
	<jsp:include page="/common/foot.jsp"></jsp:include>
</div>
	<script src='<%=basePath%>dwr/interface/routeService.js'></script>
	<script src='<%=basePath%>dwr/interface/productImportServices.js'></script>
	<script src='<%=basePath%>dwr/interface/staffService.js'></script>
	<script src='<%=basePath%>dwr/interface/wipService.js'></script>
    <script src="<%=basePath%>dwr/engine.js"></script>
    <script type="text/javascript" src="<%=basePath%>js/jquery.js"language="javascript" charset="utf-8"></script>
	<script type="text/javascript" src="<%=basePath%>js/jquery.autocomplete.js" language="javascript" charset="utf-8"></script>
	<script type="text/javascript" src="<%=basePath%>js/ftyUtils.js"language="javascript" charset="utf-8"></script>
    <script type="text/javascript" language="javascript" charset="utf-8">
        var suggestMess="<fmt:message key='route.suggest'/>";
        $(document).ready(function(){
            settedProId();
            $("#proId").autocomplete("getFtySku.do",{
             width: 345,
		     max: 10,
		     highlight: false,
		     scroll: true,
		     scrollHeight: 300
          });
        });
  
  function regRoute(){
    var routeType = $("#routeType").val();
    var proId = $("#proId").val();
    if(isEmpty(routeType)||isEmpty(proId))return;
    productImportServices.regRoute(proId,routeType,function(){
      alert("为"+proId+"设置路由【"+routeType+"】成功！");
      $("#regRouteButton").attr("disabled","disabled");
    });
  }
  
  
  function showSuggestMess(str){
    var styleNo = $("#styleNo").val();
    $("#suggest").empty();
    if(isEmpty(styleNo))return;
    if(!isEmpty(str)){
       $("#suggest").html(suggestMess.replace("{0}",str));
       return;
    }
    
    wipService.getSuggestRouteType(styleNo,function(data){
       if(!isEmpty(data)){
       $("#suggest").html(suggestMess.replace("{0}",data));
       }
    });
  }
  
  function getRouteSetting(){
         var routeType = $("#routeType").val();
         var styleNo = $("#styleNo").val();

         if(routeType==""||styleNo==""){
           $("#subBut").attr("disabled","disabled");
           $("#result").empty();
           return;
         }
         var tableResult = "<table border=1 width=800>";
         var appendResult = "";
         var celled="";
         var exec="";
         var proId = $("#proId").val();
         routeService.getRouteByRouteType(proId,routeType,function(data){
           celled="<tr>";
           appendResult = "<tr>";
           exec = "<tr>";
           var i=0;
           for(var property in data){ 
             i++;
             celled+="<td>"+(parseInt(property)+1)+"</td>";
             appendResult+="<td><input type='hidden' name='wsId' value='"+data[property].wsId+"'/>"+data[property].wsName+"</td>";
             var staffId=data[property].staffId;
             var staffName=data[property].staffName;
             if(isEmpty(staffId))staffId="";
             if(isEmpty(staffName))staffName="";
             exec+="<td >编号:<input size='3' name='staffId' id='No"+data[property].routeId+"' value='"+staffId+"' type='text' onblur='settedNo("+data[property].routeId+")'/>";
             exec+="<br>姓名:<input size='8' onblur='completeId(\""+data[property].routeId+"\");' onkeyup='settedName(\""+data[property].routeId+"\")' id='EName"+data[property].routeId+"' value='"+staffName+"' type='text'/></td>";
           }
           
           celled+="</tr>";
           appendResult+="</tr>";
           exec+="</tr>";
           tableResult+="<tr><td colspan="+i+"><b>"+routeType+"</b></td></tr>";
           tableResult+=celled;
           tableResult+=appendResult;
           tableResult+=exec;
           tableResult+="</table>";
           $("#result").empty();
           $("#result").append(tableResult);
           $("#subBut").removeAttr("disabled");
         });
       }
       
       
       function setProIdByPdt(ftySku){
         if(isEmpty(ftySku))return;
         $("#proId").val(ftySku);
         settedProId();
         $("#proIds").empty();
       }
       
       
       function settedPdtId(){
         $("#proIds").empty();
         var pdtId = $("#pdtId").val();
         if(pdtId=="")return;
         productImportServices.getProductByPdtId(pdtId,function(data){
           if(!isEmpty(data)){
             if(data.pdtskuid.indexOf(",")>0){
               for(var ftySku in data.pdtskuid.split(",")){
                 $("#proIds").append("<p><a href='#' onclick='setProIdByPdt(\""+data.pdtskuid.split(",")[ftySku]+"\")'>"+data.pdtskuid.split(",")[ftySku]+"</a></p>");
               }
             }else{
               $("#proId").val(data.pdtskuid);
               settedProId();
             }
          }
         });
       }
       
       function setStyleNo(){
         var skuNo = $("#skuNo").val();
         var styleNo=parseInt(skuNo.substring(1,8),10);
         if(!isEmpty(styleNo)){
           $("#styleNo").val(styleNo);
           getWip();
         }else{
           $("#styleNo").attr("value","");
           $("#subBut").attr("disabled","disabled");
          }
       }
       
       function settedProId(){
         var proId = $("#proId").val();
         if(proId=="")return;
         productImportServices.getProductByFtySku(proId,function(data){
           if(!isEmpty(data)){
             $("#skuNo").val(data.cmtpdtskuid);
             $("#pdtId").val(data.cmtpdtid);
             if(!isEmpty(data.routeId)){
               $("#routeType").val(data.routeId);
               $("#routeType").unbind("onchange");
               $("#routeType").unbind("change");
               $("#routeType").attr("onchange","");
               $("#routeType").selectReadOnly();
               showSuggestMess(data.routeId);
             }else{
               showSuggestMess(null);
               $("#routeType").unReadOnly();
               $("#routeType").change(getRouteSetting);
             }
             getRouteSetting();
             setStyleNo();
             $("#regRouteButton").removeAttr("disabled");
          }
         });
       }
       
       function settedSkuNo(){
         var skuNo = $("#skuNo").val();
         if(skuNo=="")return;
         productImportServices.getProId(skuNo,function(data){
           $("#proId").val(data);
           if(data.indexOf(",")<0){
            getRouteSetting();
            setStyleNo();
            $("#regRouteButton").removeAttr("disabled");
           }
         });
       }
       
       function getWip(){
          var proId = $("#proId").val();
          if(proId=="")return;
          $("#wipResult").empty();
          wipService.getWipByProSku(proId,function(data){
             var tableResult="<table border='1' width='100%'>";
             tableResult+="<tr>";
             tableResult+="<td>产品ID</td>";
             tableResult+="<td>路由名称</td>";
             tableResult+="<td>工序名称</td>";
             tableResult+="<td>操作员编号</td>";
             tableResult+="<td>操作员姓名</td>";
             tableResult+="<td>开始时间</td>";
             tableResult+="<td>结束时间</td>";
             tableResult+="</tr>";
             for(var property in data){
               tableResult+="<tr>";
               tableResult+="<td>"+data[property].proSku+"</td>";
               tableResult+="<td>"+data[property].routeType+"</td>";
               tableResult+="<td>"+data[property].wsName+"</td>";
               tableResult+="<td>"+data[property].staffId+"</td>";
               tableResult+="<td>"+data[property].staffName+"</td>";
               
               var startTimeObj = new Date(data[property].startTime);
               tableResult+="<td>"+startTimeObj.format("YYYY年MM月dd日hh点mm分ss秒")+"</td>";
               
               if(!isEmpty(data[property].endTime)){
                 var endTimeObj = new Date(data[property].endTime);
                 tableResult+="<td>"+endTimeObj.format("YYYY年MM月dd日hh点mm分ss秒")+"</td>";
               }else{
                 tableResult+="<td>执行中</td>";
               }
               
               tableResult+="</tr>";
             }
             tableResult+="</table>";
             $("#wipResult").append(tableResult);
          });
          
       }
       
       function settedNo(Id){
         var No = $("#No"+Id).val();
         staffService.getStaffName(No,function(data){
           $("#EName"+Id).val(data);
         });
       }
       
       function settedName(Id){
         var Name = $("#EName"+Id).val();
         if(isEmpty(Name))return;
         $("#EName"+Id).autocomplete("getStaffByPinyin.do",{
                width: 100,
		        max: 10,
		        highlight: false,
		        scroll: true,
		        scrollHeight: 300,
		        formatItem: function(row) {
		           return "<strong>(" + row[0] + ")</strong>: " + row[1];
	            },
	            formatResult: function (row) {
		           return row[1];
	            }
        });
       }
       
       function completeId(Id){
          var Name = $("#EName"+Id).val();
          if(isEmpty(Name))return;
          staffService.getStaffNo(Name,function(data){
           $("#No"+Id).val(data);
          });
       }
  </script>
  </body>
</html>
